// Javascript for cart
var CART_DETAIL_URL = "/index.php/checkout/cart/";
//Check product quota
function checkQuota(ProductID, MenuID, Quantity) {
    return true;
}

//Khoi tao gio hang
function initCart() {
    //Lay gio hang tu local storage.
    var cart = [];
    if (localStorage["OVI-CART"] != null) {
        cart = JSON.parse(localStorage.getItem("OVI-CART"));
    }
    //Tinh tong gia san pham.
    var totalPrice = 0;
    for (var i = 0; i < cart.length; i++) {
        totalPrice += cart[i].itemPrice * cart[i].quantity;
    }
    $("#total-price").append(totalPrice + " VND");
    $("#total-price-summary").append(totalPrice + " VND");

    //Tinh tong so luong san pham trong gio hang.
    var totalQuantity = 0;
    for (var i = 0; i < cart.length; i++) {
        totalQuantity += cart[i].quantity;
    }
    $("#total-quantity-cart").append(totalQuantity);
    $("#total-quantity-cart-detail").append("Trong giỏ (" + totalQuantity + "sản phẩm)");
    //Khoi tao html cho gio hang tren menu.
    var htmlItem = "";
    for (var i = 0; i < cart.length; i++) {
        //Lay thong tin san pham tu gio hang.
        var productID = cart[i].itemID;
        var productName = cart[i].itemName;
        var productQuantity = cart[i].quantity;
        var productIcon = cart[i].itemIcon;
        var productPrice = cart[i].itemPrice;

        //Tao HTML cho cart item.
        htmlItem += "<li><div class='clearfix'><img class='f_left m_right_10' src='" + productIcon + "'>"
                + "<div class='f_left product_description'><p class='color_dark m_bottom_5 d_block'>" + productName +
                "</p><span class='f_size_medium'>Mã sản phẩm:" + productID + "</span></div>"
                + "<div class='f_left f_size_medium'><div class='clearfix' id='cart-price-" + productID + "'>" + productQuantity + "x <b class='color_dark'>" + productPrice + "</b> VNĐ</div>"
                + "<button class='close_product color_dark tr_hover'><i class='fa fa-times'></i></button></div></div></li>";
    }
    $("#cart-menu-item").html(htmlItem);
}

function addToCart(productID) {
    //var isChange = false; //Kiem tra con hang hay khong.

    var flagIsExsit = false;
    var cart = [];
    if (localStorage["OVI-CART"] != null) {//HTML5
        cart = JSON.parse(localStorage.getItem("OVI-CART"));
    }

    for (var i = 0; i < cart.length; i++) {
        var item = cart[i];
        //Neu sp da ton tai trong gio hang => tang so luong
        //console.log("pp" + productID);
        if (item.itemID == productID) {
            //Bat flag da add sp.
            flagIsExsit = true;
            if (item.quantity < 10) {
                item.quantity++;
                //Cap nhat lai gia va so luong san pham vua them vao.
                var newHtmlPrice = item.quantity + "x <b class='color_dark'>" + item.itemPrice + "</b> VNĐ";
                $("#cart-price-" + item.itemID).html(newHtmlPrice);
            } else {
                alert("Số lượng mọi món phải nhỏ hơn 10!");
            }
            break;
        }
    }

    //Neu sp pham chua co trong gio hang (flag = false) thi them moi.
    if (!flagIsExsit) {
        //Lay icon cua san pham ve de them vao gio hang.
        var productIcon = $("#product-icon-" + productID).attr("src");
        //Lay gia san pham.
        var productPrice = $("#product-item-price-" + productID).attr("data-price");
        //Lay ten san phame
        var productName = $("#product-name-" + productID).attr("data-name");
        var item = {itemID: productID, itemName: productName, quantity: 1, itemIcon: productIcon, itemPrice: productPrice};
        cart.push(item);

        //Tao HTML cho cart item.
        var htmlItem = "<li><div class='clearfix'><img class='f_left m_right_10' src='" + productIcon + "'>"
                + "<div class='f_left product_description'><p class='color_dark m_bottom_5 d_block'>" + productName +
                "</p><span class='f_size_medium'>Mã sản phẩm:" + productID + "</span></div>"
                + "<div class='f_left f_size_medium'><div class='clearfix' id='cart-price-" + productID + "'> 1 x <b class='color_dark'>" + productPrice + "</b> VNĐ</div>"
                + "<button class='close_product color_dark tr_hover'><i class='fa fa-times'></i></button></div></div></li>";
        $("#cart-menu-item").append(htmlItem);
    }
    //Cap nhat lai tong so luong san pham va tong tien trong gio hang
    //Neu da co san pham trong gio hang thi tinh tong so luong, tong tien roi cap nhat lai.
    if (localStorage["OVI-CART"] != null) {
        //Tinh tong so luong san pham trong gio hang
        var totalQuantity = 0;
        for (var i = 0; i < cart.length; i++) {
            totalQuantity += cart[i].quantity;
        }
        //Cap nhat lai tong so luong trong gio hang
        $("#total-quantity-cart").html(totalQuantity);
        $("#total-quantity-cart-detail").html("Trong giỏ (" + totalQuantity + "sản phẩm)");
        //Tinh tong gia san pham.
        var totalPrice = 0;
        for (var i = 0; i < cart.length; i++) {
            totalPrice += cart[i].itemPrice * cart[i].quantity;
        }
        //Cap nhat lai tong gia san pham
        $("#total-price").html(totalPrice + " VND");
        $("#total-price-summary").html(totalPrice + " VND");
    } else {
        //Nguoc lai thi khoi tao so luong 1 va lay gia tri tong tien hien tai
        $("#total-quantity-cart").html(1);
        $("#total-quantity-cart-detail").html("Trong giỏ (1 sản phẩm)");
        $("#total-price").html(productPrice + " VND");
        $("#total-price-summary").html(productPrice + " VND");
    }
    //Cap nhat lai du lieu gio hang
    localStorage.setItem("OVI-CART", JSON.stringify(cart));
    //Chuyen qua trang chi tiet gio hang.
    window.location.href = CART_DETAIL_URL;
}

//Khoi tao trang chi tiet gio hang.
function initCartDetail() {
    //Lay gio hang tu local storage.
    var cart = [];
    if (localStorage["OVI-CART"] != null) {
        cart = JSON.parse(localStorage.getItem("OVI-CART"));

        //Khoi tao html cho gio hang tren menu.
        var htmlItem = "";
        for (var i = 0; i < cart.length; i++) {
            //Lay thong tin san pham tu gio hang.
            var productID = cart[i].itemID;
            var productName = cart[i].itemName;
            var productQuantity = cart[i].quantity;
            var productIcon = cart[i].itemID;
            var productPrice = cart[i].itemPrice;

            //Tao HTML cho cart item.
            htmlItem += "<tr><td style='min-width:500px'><img style='float:left;width:80px' src='" + urlimg + productIcon + ".jpg' /><p style='text-transform:uppercase;font-size:20px;margin-top:10px'>" + productName + "</p></td>"
                    + "<td>" + productQuantity + "</td><td>" + productPrice + "</td><td>" + productPrice * productQuantity + "</td></tr>";
        }
        //Cap nhat danh sach san pham vap bang gio hang.
        $("#cart-detail-product-list").html(htmlItem);

        //Cap nhat tong tien trong gio hang.
        var totalPrice = 0;
        for (var i = 0; i < cart.length; i++) {
            totalPrice += cart[i].itemPrice * cart[i].quantity;
        }
        $("#total-price-detail").append(totalPrice + " VND");
        $("#total-price-summary-detail").append(totalPrice + " VND");
    } else {
        $("#cart-detail-page").html("<h1 style='margin-top:200px;text-align:center;'>Chưa có sản phẩm trong giỏ hàng.</h1><div style='margin-left:40%;margin-bottom:200px !important;margin-top:30px'><a href='/index.php/welcome/' style='font-size:24px;'>Tiếp tục mua hàng.</a></div>");
    }
}

function updateCart(productID) {
    var value = $("#quanlity-item-" + productID).val();
    var cart = [];
    if (localStorage["OVI-CART"] != null) {
        cart = JSON.parse(localStorage.getItem("OVI-CART"));
    }

    for (var i = 0; i < cart.length; i++) {
        var item = cart[i];
        if (item.itemID == productID) {
            if (value == 0) {

                $("#add-button-" + productID).removeClass("active-btn");

                var totalItem = parseInt($("#number-of-cart-item").html().trim());
                totalItem -= item.quantity;
                $("#number-of-cart-item").html(totalItem);

                var totalPrice = parseInt($("#total-price").html().trim());
                totalPrice -= item.quantity * parseInt($("#price-cart-item-" + productID).html().trim());
                $("#total-price").html(totalPrice);

                $("#item-" + productID).remove();
                cart.splice(i, 1);

                $("#dish-in-cart-" + productID).html(0);
                $("#dish-in-cart-" + productID).css("z-index", -1);
                $("#dish-in-cart-" + productID).css("opacity", 0);
                $("#dish-in-cart-details-" + productID).css("opacity", 0);
                $("#dish-in-cart-details-" + productID).html(0);

                if (cart.length < 4) {
                    $("#control-cart").remove();
                }

                $("#slider-cart").css("left", 0);
                currentSlider = 1;

                if (cart.length == 0) {
                    showNoneFace('2', 1);
                    $("#price-table").css("visibility", "hidden");
                }

            } else {
                if (checkQuota(productID, item.menuID, value)) {
                    var totalItem = parseInt($("#number-of-cart-item").html().trim());
                    totalItem += (value - item.quantity);
                    $("#number-of-cart-item").html(totalItem);

                    var totalPrice = parseInt($("#total-price").html().trim());
                    totalPrice += (value - item.quantity) * parseInt($("#price-cart-item-" + productID).html().trim());
                    $("#total-price").html(totalPrice);

                    item.quantity = value;
                    $("#dish-in-cart-" + productID).html(item.quantity);
                    $("#dish-in-cart-details-" + productID).html(item.quantity);
                } else {
                    $("#quanlity-item-" + productID).val(item.quantity);
                    alert("Đã hết sản phẩm!");
                }
            }
            break;
        }
    }

    localStorage.setItem("OVI-CART", JSON.stringify(cart));
}
function initcheckout() {
    var cart = [];
    if (localStorage["OVI-CART"] != null) {
        cart = JSON.parse(localStorage.getItem("OVI-CART"));
      //  alert("tt");
       // var masp = "";
       // var soluong = 0;
        //Khoi tao html cho gio hang tren menu.
        var htmlItem = "";
        for (var i = 0; i < cart.length; i++) {
            //Lay thong tin san pham tu gio hang.
            var productID = cart[i].itemID;
          //  var productName = cart[i].itemName;
            var productQuantity = cart[i].quantity;
            //var productIcon = cart[i].itemID;
            var productPrice = cart[i].itemPrice;
           // soluong += cart[i].quantity;
            //masp += cart[i].itemID;
            //Tao HTML cho cart item.
//            htmlItem += "<tr><td style='min-width:500px'><img style='float:left;width:80px' src='" +urlimg+ productIcon + ".jpg' /><p style='text-transform:uppercase;font-size:20px;margin-top:10px'>" + productName + "</p></td>"
//                    + "<td>" + productQuantity + "</td><td>" + productPrice + "</td><td>" + productPrice * productQuantity + "</td></tr>";
        }
        //$("#lstmasp").val(masp);
       // $("#lstsoluong").val(soluong);
        //Cap nhat danh sach san pham vap bang gio hang.
        // $("#cart-detail-product-list").html(htmlItem);
        //Cap nhat tong tien trong gio hang.
        var totalPrice = 0;
        for (var i = 0; i < cart.length; i++) {
            totalPrice += cart[i].itemPrice * cart[i].quantity;
        }
        $("#total-price-detail").append(totalPrice + " VND");
        $("#total-price-summary-detail").append(totalPrice + " VND");
        
    }
}